//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for DropDownRange.
	/// </summary>
	public class DropDownRange : DropDownList
	{
		public DropDownRange()
		{

		}

        protected override void CreateChildControls()
        {
            base.CreateChildControls ();
        if(!Page.IsPostBack)
            {
                int current = Start;
                while(current <= Max)
                {
                    this.Items.Add(new ListItem(current.ToString(Format), current.ToString()));
                    current += Factor;
                }

                if(ShowDefault)
                {
                    if(!Globals.IsNullorEmpty(this.DefaultText))
                    {
                        this.Items.Insert(0,new ListItem(this.DefaultText,this.DefaultValue.ToString()));
                    }
                    else
                        this.Items.Insert(0,new ListItem(this.DefaultValue.ToString(Format),this.DefaultValue.ToString()));
                }
            }
        }

        public void SetSelected(int i)
        {
            this.EnsureChildControls();
            ListItem li = this.Items.FindByValue(i.ToString());
            if(li != null)
                li.Selected = true;
        }

        public int GetSelected
        {
            get{ return Int32.Parse(this.SelectedValue);}
        }


        private int _start = 0;
        private int _factor = 1;
        private int _max = 10;
        private bool _showDefault;

        private int _defaultValue = -1;
        private string _startText = null;
        private string _format = "0";

        public bool ShowDefault
        {
            get{return _showDefault;}
            set{_showDefault = value;}
        }

        public string Format
        {
            get{return _format;}
            set{_format = value;}
        }

        public string DefaultText
        {
            get{return _startText;}
            set{_startText = value;}
        }

        public int DefaultValue
        {
            get{return _defaultValue;}
            set{_defaultValue = value;}
        }

        public int Start
        {
            get{return _start;}
            set{_start = value;}
        }

        public int Factor
        {
            get{return _factor;}
            set{_factor = value;}
        }

        public int Max
        {
            get{return _max;}
            set{_max = value;}
        }
	}
}
